<%
System.out.println("DENTRO DE JAVA");
String sPk = "'"+request.getParameter("PK")+"'";
String sUsuario =  "'"+request.getParameter("User")+"'";
String tituloCorto = "'"+request.getParameter("Titulo")+"'";
%>

<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=7" />
		<title>Custom Pruebas Santillana</title>
		<script type="text/javascript"  src="/suppliers/js/dojo-release-1.6.1/dojo/dojo.js" djConfig="parseOnLoad:true"></script> 
		<script type="text/javascript">
			
			//IMPORTACION DE LIBRERIAS
			dojo.require("dijit.dijit"); // optimize: load dijit layer
			dojo.require("dijit.form.Form");
			dojo.require("dijit.form.Button");
			dojo.require("dijit.form.TextBox");
			dojo.require("dijit.form.DateTextBox");
			dojo.require("dijit.form.Select");
			dojo.require("dijit.form.ComboBox");
			dojo.require("dijit.form.FilteringSelect");
			dojo.require("dojo.store.Memory");
			
			dojo.require("dijit.Declaration");
			//Librerias Grid
			dojo.require("dojox.grid.EnhancedGrid");
			dojo.require("dojo.data.ItemFileReadStore");
			dojo.require("dojox.grid.enhanced.plugins.IndirectSelection");
			dojo.require("dojox.grid.enhanced.plugins.Pagination"); 
			dojo.require("dojo.data.ItemFileWriteStore");
			dojo.require("dijit.layout.SplitContainer");
            dojo.require("dijit.layout.TabContainer");
            dojo.require("dijit.layout.ContentPane");
            dojo.require("dijit.TitlePane");
			dojo.require("dijit.Dialog");
			//Create Tree
			dojo.require("dojo.data.ItemFileReadStore");				
			dojo.require("dijit.tree.TreeStoreModel");
			dojo.require("dijit.Tree");
			
			//Librerias LazyTreeGrid
			dojo.require("dojox.grid.LazyTreeGrid");
			dojo.require("dijit.tree.ForestStoreModel");

			//Libreria JSON
			dojo.require("dojox.json.ref");

//-----------------------INICIO BLOQUE CONTROL VISUALIZACION DE DIVISIONES----------------------------
			// function getStyleObject(string) -> returns style object
			//  given a string containing the id of an object
			//  the function returns the stylesheet of that object
			//  or false if it can't find a stylesheet.  Handles
			//  cross-browser compatibility issues.
			function getStyleObject(objectId) {
				// checkW3C DOM, then MSIE 4, then NN 4.
				if(document.getElementById && document.getElementById(objectId)) {
				return document.getElementById(objectId).style;
				}
				else if (document.all && document.all(objectId)) {
					return document.all(objectId).style;
				} 
				else if (document.layers && document.layers[objectId]) {
					return document.layers[objectId];
				} else {
					return false;
				}
			}
			function changeObjectVisibility(objectId, newVisibility) {
				// first get a reference to the cross-browser style object
				// and make sure the object exists
				var styleObject = getStyleObject(objectId);
				if(styleObject) {
					styleObject.visibility = newVisibility;
					return true;
				} else {
					// we couldn't find the object, so we can't change its visibility
					return false;
				}
			}
			function hideElement(ele_id)
			{
				changeObjectVisibility(ele_id,"hidden");
			}
			function showElement(ele_id)
			{
				changeObjectVisibility(ele_id,"visible");
			}
//---------------------FIN DEL BLOQUE DE CONTROL DE VISUALIZACION DE DIVISIONES-------------------
	
			function cerrarVentana(){
				javascript:window.close();
			}
			function abrirVentana(url){
				
				//window.open(url,'_blank','width=800,height=600,menubar=no,personalbar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=yes,top=200,left=200');
				window.top.location.href = url;
			}
			function extenderArticulo() {
				var pkSociedad = dijit.byId("sociedadesSelect").get("value");
				var sPk = <%=sPk%>;
				document.body.style.cursor = 'wait';
				try {
					dojo.xhrGet({					
						url: "/extenderArticulo.wpc",
						handleAs: 'json',
						timeout: 60000,
						content:{pkArticulo: sPk, pkSociedad: pkSociedad},
						load: function(response, ioArgs){
				        	if(response[0].url!=""){
				        		abrirVentana(response[0].url);
				        	}
				        	else{
							}
				        	return true;
				        },
				        error: function(response, ioArgs){
				        	//alert(response);
				          	console.log("HTTP status code: ", ioArgs.xhr.status);
				          	return null;
				        }
					});
				}
				catch(err) {
					alert(" Error en ExtenderArticulo: " + err.message);
				}
			}
			
			dojo.addOnLoad(function() {
				var sUrl = document.URL;
				var sPk = <%=sPk%>;
				var sociedadesListJSON;
				
				dojo.xhrGet({					
					url: "/obtenerSociedadesNoMapeadas.wpc",
					handleAs: 'json',
					sync: true,
					timeout: 60000,
					content:{PK: sPk,URL: sUrl},
					load: function(response, ioArgs){
			        	if(response==""){
			        		alert('No hay datos.');
			        	}
			        	else{				        	
			        		//alert(response);
			        		showElement("tp3");
			        		sociedadesListJSON = {identifier: "id", label: "sociedad", items: response};			        		
			        		
/* 			        		var store = new dojo.data.ItemFileReadStore({ data: sociedadesListJSON});
			        		store.fetch({onComplete: function(items) {
									alert("num. items="+items.length);
						
					    			alert("id="+store.getValue(items[0],"id"));
					    			alert("sociedad="+store.getValue(items[0],"sociedad"));
								}					
							});
 */						
 						}
			        	return false;
			        },
			        error: function(response, ioArgs){
			        	//alert(response);
			          	console.log("HTTP status code: ", ioArgs.xhr.status);
			          	return null;
			        }
				});

				var storeSociedades = new dojo.data.ItemFileReadStore({ data: sociedadesListJSON});
/* 				storeSociedades.fetch({onComplete: function(items) {
					alert("num. items="+items.length);
		
	    			alert("id="+storeSociedades.getValue(items[0],"id"));
	    			alert("sociedad="+storeSociedades.getValue(items[0],"sociedad"));
					}					
				});				
 */				
 				
 				dojo.byId("titulocorto").innerHTML = "Titulo del articulo: " + <%=tituloCorto%>;											
				
				var cbSociedades = new dijit.form.FilteringSelect({
		            	id: "sociedadesSelect",
		            	name: "sociedades",
		            	store: storeSociedades,
		            	searchAttr: "sociedad",
		            	style: "width: 20em;",
		            	placeHolder: "Seleccionar una sociedad"
/* 		            	onChange: function(){
							dojo.byId("valorSeleccionado").innerHTML = dijit.byId("sociedadesSelect").get("value");
						}
 */		        	},
		        	"sociedadesSelect"
		     	);
				cbSociedades.startup();

				hideElement("tp3");
				
			});
		</script>				
		
		
        <style type="text/css">
			body, html, table { font-family:helvetica,arial,sans-serif; font-size:83%; }
        </style>
</head>

<link rel="stylesheet" type="text/css" href="/suppliers/js/dojo-release-1.6.1/dijit/themes/claro/claro.css"/>
<style type="text/css">
	@import "/suppliers/js/dojo-release-1.6.1/dojox/grid/enhanced/resources/claro/EnhancedGrid.css";
	@import "/suppliers/js/dojo-release-1.6.1/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css";
	.dojoxGrid table { margin: 0; } html, body { width: 100%; height: 100%; margin: 0; }
	  	@import "/suppliers/js/dojo-release-1.6.1/dijit/themes/claro/layout/ContentPane.css";
	  	@import "/suppliers/js/dojo-release-1.6.1/dijit/themes/claro/layout/TabContainer.css";
	.claro .dijitDialog {
		background: none repeat-x scroll left top #FFFFFF;
	}
	.dijitTitlePaneContentInner .dijitTitlePaneSingleChild{
		height: 295px!important;
	    width: 100%!important;
	    overflow-x: scroll !important;
	    overflow-y: visible !important;
	}


 </style>
<body class="claro" style="width:90%;height:90%;">

	<div style="width: 100%; margin:1em;" >
	
		<img src="/suppliers/MDM/ctg_files/logo.png" alt="Imagen del articulo." width="92" height="22">
		
		<label id="titulo" for="titulo" style="margin-left: 5em;font-size: 150%; font-weight:bold">
			Extender Articulo
		</label>
	</div>

	<div style="border: 1px solid black"></div>
	
	<div style="width: 100%; margin: 1em">
    	<label id="titulocorto" for="titulocorto" ></label>
	</div>

	<div style="width: 100%; margin: 1em">
		<input id="sociedadesSelect">
		
		<button data-dojo-type="dijit.form.Button" data-dojo-props="onClick:function(){extenderArticulo();}" type="button">
		 		Extender
		 </button>
	</div>

</body>
	
</html>
